package com.cskaoyan.gateway.controller.shopping;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.ICartService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.DeleteCartItemRequest;
import com.mall.shopping.dto.DeleteCartItemResponse;
import com.mall.shopping.dto.DeleteCheckedItemRequest;
import com.mall.shopping.dto.DeleteCheckedItemResposne;
import com.mall.user.intercepter.TokenIntercepter;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 *
 * created by feng on 7/22/21
 */
@RequestMapping("/shopping")
@RestController
public class CartController {

    @Reference(interfaceClass = ICartService.class,timeout = 3000, check = false)
    ICartService iCartService;

    @DeleteMapping("carts/{uid}/{pid}")
    public ResponseData deleteCartItem(@PathVariable("uid") Long uid, @PathVariable("pid") Long pid){
        DeleteCartItemRequest deleteCartItemRequest = new DeleteCartItemRequest();
        deleteCartItemRequest.setUserId(uid);
        deleteCartItemRequest.setItemId(pid);
        DeleteCartItemResponse deleteCartItemResponse = iCartService.deleteCartItem(deleteCartItemRequest);
        if(ShoppingRetCode.SUCCESS.getCode().equals(deleteCartItemResponse.getCode())){
            return new ResponseUtil<>().setData("成功");
        }
        return new ResponseUtil<>().setErrorMsg(deleteCartItemResponse.getMsg());
    }

    @DeleteMapping("items/{id}")
    public ResponseData deleteCartItem(HttpServletRequest request){

        String token = (String)request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        long uid = Long.parseLong(token);

        DeleteCheckedItemRequest deleteCheckedItemRequest = new DeleteCheckedItemRequest();
        deleteCheckedItemRequest.setUserId(uid);
        DeleteCheckedItemResposne deleteCheckedItemResposne = iCartService.deleteCheckedItem(deleteCheckedItemRequest);



        if(ShoppingRetCode.SUCCESS.getCode().equals(deleteCheckedItemResposne.getCode())){
            return new ResponseUtil<>().setData("成功");
        }
        return new ResponseUtil<>().setErrorMsg(deleteCheckedItemResposne.getMsg());
    }
}